unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

const
  N = 30;

type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure TBitBtn1Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  arr : array [0..(N*N-1)] of TBitBtn;
  turn, x, o : integer;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  i, a: integer;
begin
  for i:=0 to N*N-1 do
  begin
    arr[i]:= TBitBtn.Create(Form1);
    arr[i].Width:=20;
    arr[i].Height:=20;
    arr[i].Left:= 50 + (i mod N)*20;
    arr[i].Top:= 50 + (i div N)*20;
    arr[i].Parent:=Form1;
    arr[i].OnClick:=TBitBtn1Click;
  end;
end;

procedure TForm1.TBitBtn1Click(Sender: TObject);
var
  i, j, a, b, k: integer;
begin
  Form1.Label2.Caption:=IntToStr(x)+':'+IntToStr(o);
  for i:=0 to N*N-1 do
  begin
    if (Sender = arr[i]) and (arr[i].Caption = '') then
    begin
      if (turn mod 2 = 0) then
      begin
        arr[i].Font.Color:=clRed;
        arr[i].Caption:='X';
      end else begin
        arr[i].Font.Color:=clBlue;
        arr[i].Caption:='O';
      end;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        a:=a+1;
        if (j mod N) <> 0 then
        begin
          j:=j-1;
        end else begin
          break;
        end;
      end;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        b:=b+1;
        if (j mod N) <> (N-1) then
        begin
          j:=j+1;
        end else begin
          break;
        end;
      end;
      if (a+b+1 >= 5) then
      begin
        for j:=(i-a) to (i+b) do
        begin
          arr[j].Font.Style:=[fsBold];
        end;
        Application.MessageBox('X wins!', 'Congratulations!');
        for j:=0 to N*N-1 do
        begin
          arr[j].Caption:= '';
          arr[j].Font.Style:=[];
          arr[j].Enabled:=True;
        end;
        x:=x+1;
        Form1.Label2.Caption:=IntToStr(x)+':'+IntToStr(o);
        turn:=-1;
      end;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        a:=a+1;
        if (j mod N) <> 0 then
        begin
          j:=j-1;
        end else begin
          break;
        end;
      end;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        b:=b+1;
        if (j mod N) <> (N-1) then
        begin
          j:=j+1;
        end else begin
          break;
        end;
      end;
      if (a+b+1 >= 5) then
      begin
        for j:=(i-a) to (i+b) do
        begin
          arr[j].Font.Style:=[fsBold];
        end;
        Application.MessageBox('O wins!', 'Congratulations!');
        for j:=0 to N*N-1 do
        begin
          arr[j].Caption:= '';
          arr[j].Font.Style:=[];
          arr[j].Enabled:=True;
        end;
        o:=o+1;
        Form1.Label2.Caption:=IntToStr(x)+':'+IntToStr(o);
        turn:=-1;
      end;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        a:=a+1;
        if j>=N then
        begin
          j:=j-N;
        end;
      end;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        b:=b+1;
        if j < N*(N-1) then
        begin
          j:=j+N;
        end;
      end;
      if (a+b+1 >= 5) then
      begin
        k:=i-a*N;
        for j:=0 to (a+b) do
        begin
          arr[k].Font.Style:=[fsBold];
          k:=k+N;
        end;
        Application.MessageBox('O wins!', 'Congratulations!');
        for j:=0 to N*N-1 do
        begin
          arr[j].Caption:= '';
          arr[j].Font.Style:=[];
          arr[j].Enabled:=True;
        end;
        o:=o+1;
        Form1.Label2.Caption:=IntToStr(x)+':'+IntToStr(o);
        turn:=-1;
      end;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        a:=a+1;
        j:=j-N;
      end;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        b:=b+1;
        j:=j+N;
      end;
      if (a+b+1 >= 5) then
      begin
        k:=i-a*N;
        for j:=0 to (a+b) do
        begin
          arr[k].Font.Style:=[fsBold];
          k:=k+N;
        end;
        Application.MessageBox('X wins!', 'Congratulations!');
        for j:=0 to N*N-1 do
        begin
          arr[j].Caption:= '';
          arr[j].Font.Style:=[];
          arr[j].Enabled:=True;
        end;
        x:=x+1;
        Form1.Label2.Caption:=IntToStr(x)+':'+IntToStr(o);
        turn:=-1;
      end;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        a:=a+1;
        if (j mod N) <> 0 then
        begin
          j:=j-N-1;
        end else begin
          break;
        end;
      end;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        b:=b+1;
        if (j mod N) <> N-1 then
        begin
          j:=j+N+1;
        end else begin
          break;
        end;
      end;
      if (a+b+1 >= 5) then
      begin
        k:=i-a*(N+1);
        for j:=0 to (a+b) do
        begin
          arr[k].Font.Style:=[fsBold];
          k:=k+N+1;
        end;
        Application.MessageBox('X wins!', 'Congratulations!');
        for j:=0 to N*N-1 do
        begin
          arr[j].Caption:= '';
          arr[j].Font.Style:=[];
          arr[j].Enabled:=True;
        end;
        x:=x+1;
        Form1.Label2.Caption:=IntToStr(x)+':'+IntToStr(o);
        turn:=-1;
      end;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        a:=a+1;
        if (j mod N) <> 0 then
        begin
          j:=j-N-1;
        end else begin
          break;
        end;
      end;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        b:=b+1;
        if (j mod N) <> (N-1) then
        begin
          j:=j+N+1;
        end else begin
          break;
        end;
      end;
      if (a+b+1 >= 5) then
      begin
        k:=i-a*(N+1);
        for j:=0 to (a+b) do
        begin
          arr[k].Font.Style:=[fsBold];
          k:=k+N+1;
        end;
        Application.MessageBox('O wins!', 'Congratulations!');
        for j:=0 to N*N-1 do
        begin
          arr[j].Caption:= '';
          arr[j].Font.Style:=[];
          arr[j].Enabled:=True;
        end;
        o:=o+1;
        Form1.Label2.Caption:=IntToStr(x)+':'+IntToStr(o);
        turn:=-1;
      end;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        a:=a+1;
        if (j mod N) <> 0 then
        begin
          j:=j+N-1;
        end else begin
          break;
        end;
      end;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        b:=b+1;
        if (j mod N) <> (N-1) then
        begin
          j:=j-(N-1);
        end else begin
          break;
        end;
      end;
      if (a+b+1 >= 5) then
      begin
        k:=i-a*(N-1);
        for j:=0 to (a+b) do
        begin
          arr[k].Font.Style:=[fsBold];
          k:=k+N-1;
        end;
        Application.MessageBox('X wins!', 'Congratulations!');
        for j:=0 to N*N-1 do
        begin
          arr[j].Caption:= '';
          arr[j].Font.Style:=[];
          arr[j].Enabled:=True;
        end;
        x:=x+1;
        Form1.Label2.Caption:=IntToStr(x)+':'+IntToStr(o);
        turn:=-1;
      end;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        a:=a+1;
        if (j mod N) <> 0 then
        begin
          j:=j+N-1;
        end else begin
          break;
        end;
      end;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        b:=b+1;
        if (j mod N) <> (n-1) then
        begin
          j:=j-(N-1);
        end else begin
          break;
        end;
      end;
      if (a+b+1 >= 5) then
      begin
        k:=i-a*(N-1);
        for j:=0 to (a+b) do
        begin
          arr[k].Font.Style:=[fsBold];
          k:=k+N-1;
        end;
        Application.MessageBox('O wins!', 'Congratulations!');
        for j:=0 to N*N-1 do
        begin
          arr[j].Caption:= '';
          arr[j].Font.Style:=[];
          arr[j].Enabled:=True;
        end;
        o:=o+1;
        Form1.Label2.Caption:=IntToStr(x)+':'+IntToStr(o);
        turn:=-1;
      end;
      turn:=turn+1;
    end;
  end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var j: integer;
begin
  for j:=0 to N*N-1 do
  begin
    arr[j].Caption:= '';
    arr[j].Enabled:=True;
    Form1.Label2.Caption:='0:0';
  end;
  turn:=0;
end;

end.
